//Dependency Injection

//构造函数注入
interface IWeapon {
  Hit(target: string): void;
}
class Sword implements IWeapon {
  Hit(target: string): void {
    console.log('hit ' + target);
  }
}
class Samurai {
  private weapon!: IWeapon;
  constructor(weapon?: IWeapon) {
    if (weapon) this.weapon = weapon;
  }
  setWeapon(weapon: IWeapon) {
    this.weapon = weapon;
  }
  Attack(target: string) {
    this.weapon.Hit(target);
  }
}
class Program {
  main() {
    let warrior = new Samurai(new Sword());
    warrior.Attack('evildoers');
  }
}
let p = new Program();
p.main();
